package client.layer;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import server.layer.Person;
import server.layer.ServiceProtocol;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * User: Awake
 * Date: 25.03.12
 * Time: 16:40
 * To change this template use File | Settings | File Templates.
 */
public class ClientController {
    static ServiceClient clientConnection;

    public static void connect() {
        clientConnection = new ServiceClient();
        sendQuery(ServiceProtocol.CONNECT);
    }

    public static void receive(String str) {


        try {
            JSONObject fullStr = new JSONObject(str);
            ServiceProtocol command = ServiceProtocol.getCommand(fullStr.getString("command"));
            switch (command) {
                case UPDATE_LIST:
                    receiveList(fullStr);
                    break;
                case SEND_DESCRIPTION:
                    receiveDescription(fullStr);
                    break;
                case SEND_IMAGE:
                    receiveImage(fullStr);
                    break;
            }

        } catch (JSONException e1) {
            e1.printStackTrace();
        }
    }

    public static void receiveList(JSONObject fullStr) {
        MyModel.personList.clear();
        try {
            JSONArray jArray = fullStr.getJSONArray("array");
            for (int i = 0; i < jArray.length(); i++) {
                JSONObject object = jArray.getJSONObject(i);
                Person person = getPerson(object);
                MyModel.personList.add(person);
                ViewPanel.tableModel.fireTableDataChanged();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public static void receiveDescription(JSONObject object) {
        String description = "";
        try {
            Long id = object.getLong("id");
            description = (object.has("description") && object.getString("description") != null) ? object.getString("description") : "";
            ViewPanel.description.setText(description);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public static void receiveImage(JSONObject object) {
        try {
            JSONArray jImg = object.getJSONArray("img");
            byte[] img = new byte[jImg.length()];
            for (int i = 0; i < jImg.length(); i++) {
                img[i] = (byte) jImg.getInt(i);
            }
            InputStream in = new ByteArrayInputStream(img);
            BufferedImage image = ImageIO.read(in);
            ViewPanel.imagePanel.setImage(image);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public static Person getPerson(JSONObject object) {
        Person person = new Person();
        {
            try {
                if (object.has("person_id")) {
                    person.setPerson_id(object.getLong("person_id"));
                }
                if (object.has("lName")) {
                    person.setFirstName(object.getString("fName"));
                }
                if (object.has("lName")) {
                    person.setLastName(object.getString("lName"));
                }
                if (object.has("birthDate")) {
                    Long time = object.getLong("birthDate");
                    Date date = new Date(time);
                    person.setBirthDate(date);
                }
                if (object.has("eMail")) {
                    person.seteMail(object.getString("eMail"));
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return person;
    }


    public static void requestDescription(Long person_id) {
        sendQuery(ServiceProtocol.GET_DESCRIPTION, person_id);
    }

    public static void requestImage(Long person_id) {
        sendQuery(ServiceProtocol.GET_IMAGE, person_id);
    }

    public static void requestDeletePerson(Long person_id) {
        sendQuery(ServiceProtocol.DELETE_PERSON, person_id);
    }

    public static void requestAddPerson(Long person_id) {
        sendQuery(ServiceProtocol.ADD_PERSON, person_id);
    }

    public static void requestModifyPerson(Person person) {
        sendQuery(ServiceProtocol.MODIFY_PERSON, person);
    }

    public static void sendNewDescription(String description, Long person_id) {
        sendQuery(ServiceProtocol.SEND_DESCRIPTION, person_id);
    }

    public static void sendNewImage(byte[] imgData, Long person_id){
        sendQuery(ServiceProtocol.SEND_IMAGE, imgData);
    }

    public static void sendQuery(ServiceProtocol command, Object arg) {
        JSONObject obj = new JSONObject();
        JSONObject argument = new JSONObject(arg);
        System.out.println(argument);
        try {
            obj.put("command", command.getAbbr());
            obj.put("argument", argument);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        clientConnection.sendMessage(obj.toString());
    }

    public static void sendQuery(ServiceProtocol command, Long arg) {
        JSONObject obj = new JSONObject();
        try {
            obj.put("command", command.getAbbr());
            obj.put("argument", arg);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        clientConnection.sendMessage(obj.toString());
    }

    public static void sendQuery(ServiceProtocol command) {
        JSONObject obj = new JSONObject();
        try {
            obj.put("command", command.getAbbr());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        clientConnection.sendMessage(obj.toString());
    }

    public static void closeConnection() {
        if (clientConnection != null) {
            sendQuery(ServiceProtocol.CLOSE_CONNECTION);
            clientConnection.kill();
        }
    }
}
